<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格"
          prop="goods_price"
          width="100px"
        ></el-table-column>
        <el-table-column
          label="商品数量"
          prop="goods_number"
          width="100px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="100px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="upd_time " width="100px">
          <template #default="scope">
            {{ scope.row.upd_time | dataFormate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 商品编辑对话框 -->
    <el-dialog
      title="编辑商品信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close='resetEditForm'
    >
      <el-form
        :model="editGoods"
        :rules="goodsRules"
        ref="goodsFormRef"
        label-width="100px"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editGoods.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="editGoods.goods_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="库存">
          <el-input v-model="editGoods.goods_number" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量">
          <el-input v-model="editGoods.goods_weight" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendEditGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
    
<script>
export default {
  data() {
    return {
      // 商品查询参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },

      // 商品列表数据
      goodsList: [],

      // 商品数据条数
      total: 0,

      // 添加商品数据
      addGoodsForm: {},

      // 商品编辑对话框展示与隐藏
      editDialogVisible: false,

      // 编辑商品数据对象
      editGoods: {
        goods_name: "",
        goods_price: null,
        goods_number: null,
        goods_weight: null,
        
      },

      // 当前编辑商品 id
      edit_goods_id: null,

      // 表单验证规则
      goodsRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {
    this.getGoodsList();
  },
  methods: {
    // 获取商品数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });

      if (res.meta.status !== 200) return this.$message.error("获取商品失败");
      this.$message.success("获取商品列表成功");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      this.queryInfo.pagenum = parseInt(res.data.pagenum);
      //   console.log(res.data)
    },

    // 监听分页数改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },

    // 监听当前页码值改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },

    // 根据 id 删除对应商品
    async removeGoods(goodsId) {
      // 詢問用戶是否刪除該商品
      const value = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      if (value === "cancel") {
        return this.$message.info("已取消刪除");
      }
      const { data: res } = await this.$http.delete("goods/" + goodsId);

      if (res.meta.status !== 200) return this.$message.error("刪除失敗");
      this.$message.success("刪除成功");
      this.getGoodsList();
    },

    // 点击编辑 展示编辑商品对话框
    async showEditDialog(row) {
      this.edit_goods_id = row.goods_id;
console.log(row)
      this.editGoods.goods_name = row.goods_name;
      this.editGoods.goods_price = row.goods_price;
      this.editGoods.goods_weight = row.goods_weight;
      this.editGoods.goods_number = row.goods_number;
      this.editDialogVisible = true;
    },

    // 点击确定 发起编辑商品请求
    sendEditGoods() {
      // 请求前 表单预验证
      this.$refs.goodsFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error("请检查内容后重试");
        }
        const { data: res } = await this.$http.put(
          "goods/" + this.edit_goods_id,
          this.editGoods
        );
        console.log(res);
        if (res.meta.status !== 201) return this.$message.error("编辑失败");
        this.$message.success("编辑商品成功");
        this.getGoodsList();
      });
    },

    // 监听编辑商品对话框关闭事件
    resetEditForm(){
      this.$refs.goodsFormRef.resetFields()
    },

    // 點擊添加商品  利用編程式導航跳轉到添加商品頁面
    addGoods() {
      this.$router.push("/add");
    },
  },
};
</script>
    
<style lang='less' scoped>
</style>